﻿Imports Llama
Imports Telerik.WinControls.UI
Imports SPF.Client.Administration
Imports SPF.Client
Imports SPF.Client.Schema.Collection
Imports System.Xml
Imports SPF.Client.Schema.Interface
Imports System.Text.RegularExpressions
Module Module2
    Public Sub UpdateSize(SPID As String, OldSize As String, NewSize As String, UID As String, UIDInSPI As String)
        Dim objInst As LMInstrument
        objInst = DS.GetInstrument(SPID)
        If IsNothing(objInst) Then
            Exit Sub
        End If
        If NewSize <> "0" Then
            objInst.Attributes("NominalDiameter").Value = NewSize & UID
            For Each objPipingPoint As LMPipingPoint In objInst.PipingPoints
                objPipingPoint.Attributes("NominalDiameter").Value = NewSize & UID
                objPipingPoint.Commit()   'commit不能离属性修改的属性太远
                '找两个端点的坐标，放置大小头
            Next
            Writelog("仪表 [" & objInst.Attributes("ItemTag").Value & "], 口径由 [" & OldSize & "] 更新为 [" & objInst.Attributes("NominalDiameter").Value & "]", "反向数据流")
            '还需要end 1 和 end 2 的两个size
        End If
        objInst.Commit()
        objInst = Nothing
    End Sub

    ''' <summary>
    ''' 1，代表执行机构。2，代表阀门类型。3，代表流量计类型
    ''' </summary>
    ''' <param name="SPID"></param>
    ''' <param name="NewFileName"></param>
    ''' <param name="mode"></param>
    ''' <remarks></remarks>
    Public Sub UpdateSymbolWithDwgOpened(SPID As String, NewFileName As String, mode As Integer)
        Dim oPlace As Plaice.Placement = New Plaice.Placement
        Dim oSymbol As LMSymbol : Dim oSymbol2 As LMSymbol : Dim sym_spid As String = ""
        If mode = 1 Then 'actuator
            SelectStr = <string>select t2.itemtag,t8.sp_id ActSymID
        from xxxSchemaName.t_instrument t1
        join xxxSchemaName.t_modelitem t3 on t1.sp_id=t3.sp_id
        join xxxSchemaName.t_plantitem t2 on t1.sp_id=t2.sp_id
        join xxxSchemaName.t_representation t6 on t6.sp_modelitemid=t1.sp_id
        left join xxxSchemaName.t_inlinecomp t4 on t4.sp_id=t1.sp_id
    join xxxSchemaName.T_Drawing t5 on t5.SP_ID=t6.SP_DrawingID
    left join xxxSchemaName.t_plantitem t7 on t2.sp_id=t7.sp_partofid
    left join xxxSchemaName.t_representation t8 on t8.sp_modelitemid=t7.sp_id
        where t2.sp_id= 'xxxSPID'
</string>.Value
        ElseIf mode = 2 Or mode = 3 Then 'bodytype
            SelectStr = <string>
select t2.itemtag,t6.sp_id SymID,t6.filename FileName
        from xxxSchemaName.t_instrument t1
        join xxxSchemaName.t_plantitem t2 on t1.sp_id=t2.sp_id
        join xxxSchemaName.t_modelitem t3 on t1.sp_id=t3.sp_id
        join xxxSchemaName.t_representation t6 on t6.sp_modelitemid=t1.sp_id
where t2.sp_id= 'xxxSPID'   
</string>.Value
        End If

        SelectStr = SelectStr.Replace("xxxSPID", SPID)
        Dim dtCreatePID As New DataTable
        MyPIDConn.PIDReadDataByDBA(MyPIDConn.PIDPlant_DBNAME, MyPIDConn.ActivePlantPIDApp_User, SelectStr, dtCreatePID)
        Try
            If dtCreatePID.Rows.Count = 0 Then
                Exit Sub
            End If

            If mode = 1 Then
                sym_spid = dtCreatePID(0)("actsymid")
            ElseIf mode = 2 Or mode = 3 Then
                sym_spid = dtCreatePID(0)("symid")
            End If
            oSymbol = DS.GetSymbol(dtCreatePID(0)(1).ToString)   '有时候这个地方会抱错，感觉是开了pid又关了之后
            oSymbol2 = oPlace.PIDReplaceSymbol(NewFileName, oSymbol)   '这样必须要开着图
        Catch
            If mode = 1 Then 'actuator
                Writelog("仪表 [" & dtCreatePID(0)(0) & "],执行机构 ,未能更新,因为图纸未打开。", "反向数据流")
                '                SelectStr = <string>
                '                              update xxxSchemaName.T_REPRESENTATION
                'set filename='zzz',itemstatus='4' where sp_id='yyy'
                '                          </string>.Value.Replace("zzz", NewFileName).Replace("yyy", sym_spid)
                '                MyPIDConn.PIDUpdateDataByDBA(MyPIDConn.ActivePlantPIDApp_User, SelectStr)
            ElseIf mode = 2 Then
                '                SelectStr = <string>
                '                              update xxxSchemaName.T_REPRESENTATION
                'set filename='zzz' where sp_id='yyy'
                '                          </string>.Value.Replace("zzz", NewFileName).Replace("yyy", sym_spid)
                '                MyPIDConn.PIDUpdateDataByDBA(MyPIDConn.ActivePlantPIDApp_User, SelectStr)
                Writelog("仪表 [" & dtCreatePID(0)(0) & "],阀体类型 ,未能更新,因为图纸未打开。", "反向数据流")
            ElseIf mode = 3 Then
                Writelog("仪表 [" & dtCreatePID(0)(0) & "],流量计形式 ,未能更新,因为图纸未打开。", "反向数据流")
            End If

        End Try
        oPlace = Nothing
    End Sub

    Public Sub UpdateMoreProp(SPID As String, NewValue As String, PropNameInPID As String)
        Dim objInst As LMInstrument
        Dim oldSize As String
        Dim NumberInStr As String
        Dim AlphaInStr As String
        Dim OldValueUoM As String
        objInst = DS.GetInstrument(SPID)
        If IsNothing(objInst) Then
            Exit Sub
        End If
        Dim objAttr As New LMAAttribute
        objAttr = objInst.Attributes(PropNameInPID)
        If IsNothing(objAttr) Then
            Writelog("仪表 [" & objInst.Attributes("ItemTag").Value & "],属性 [" & PropNameInPID & "] 不存在。无法更新。", "反向数据流")
            Exit Sub
        End If

        If objInst.Attributes(PropNameInPID).Value Is System.DBNull.Value Then
            oldSize = ""
        Else
            oldSize = objInst.Attributes(PropNameInPID).Value
        End If
        If IsNumeric(Mid(NewValue, 1, 1)) Then   '如果是数值
            '可以考虑加空格，增加写入的成功率
            NumberInStr = Regex.Replace(NewValue, "[^\d.\d]", "")
            AlphaInStr = Regex.Replace(NewValue, "[\d.\d]", "")
            Try
                OldValueUoM = Regex.Replace(objInst.Attributes(PropNameInPID).Value, "[\d.\d]", "")
                If AlphaInStr.ToUpper = OldValueUoM.ToUpper Then
                    NewValue = NumberInStr.Replace(" ", "") & " " & OldValueUoM.Replace(" ", "")
                Else
                    'uom is not the same
                End If
                'Dim X = objInst.Attributes(PropNameInPID).ISPAttribute.Format.FormatElements.Count
                'For I = 1 To X
                '    Debug.Print(objInst.Attributes(PropNameInPID).ISPAttribute.Format.FormatElements.Item(I).Name)
                'Next
                'Dim Y = objInst.Attributes(PropNameInPID).ISPAttribute.Format.FormatType

            Catch ex As Exception
                NewValue = NumberInStr.Replace(" ", "") & " " & AlphaInStr.Replace(" ", "")
            End Try


        Else
            '如果本来就是字母开头的就算了

        End If
        Try
            '考虑单位  比如kpa和kPa的匹配

            objInst.Attributes(PropNameInPID).Value = NewValue
            objInst.Commit()
            If objInst.Attributes(PropNameInPID).Value = NewValue Then
                Writelog("仪表 [" & objInst.Attributes("ItemTag").Value & "],属性 [" & PropNameInPID & "] ,由 [" & oldSize & "] 更新为 [" & objInst.Attributes(PropNameInPID).Value & "]", "反向数据流")
            Else
                Writelog("仪表 [" & objInst.Attributes("ItemTag").Value & "],属性 [" & PropNameInPID & "] ,未能由 [" & oldSize & "] 更新为 [" & NewValue & "]", "反向数据流")
            End If
        Catch
            Writelog("仪表 [" & objInst.Attributes("ItemTag").Value & "],属性 [" & PropNameInPID & "] ,未能由 [" & oldSize & "] 更新为 [" & NewValue & "]", "反向数据流")

        End Try


        objInst = Nothing

    End Sub



    Public Sub UpdateConntoProcessSize(TagName As String, SPID As String, OldSize As String, NewValue As String)

        Dim objPipe As LMPipeRun
        'Dim objPipingComp As LMPipingComp
        Dim NumberInStr As String
        Dim AlphaInStr As String
        objPipe = DS.GetPipeRun(SPID)
        If Not IsNothing(objPipe) Then
            Exit Sub
        End If
        If objPipe.Attributes("NominalDiameter").Value Is System.DBNull.Value Then
            OldSize = ""
        Else
            OldSize = objPipe.Attributes("NominalDiameter").Value
        End If
        If IsNumeric(Mid(NewValue, 1, 1)) Then
            NewValue = NewValue.ToLower
            If InStr(NewValue, "mm") > 0 Or InStr(NewValue, " mm") > 0 Then
                'mm
                '可以考虑加空格，增加写入的成功率
                NumberInStr = Regex.Replace(NewValue, "[^\d.\d]", "")
                AlphaInStr = Regex.Replace(NewValue, "[\d.\d]", "")
                NewValue = NumberInStr.Replace(" ", "") & " " & AlphaInStr.Replace(" ", "")
            ElseIf InStr(NewValue, "in") > 0 Or InStr(NewValue, """") > 0 Then
                NumberInStr = Regex.Replace(NewValue, "[^\d.\d]", "")
                AlphaInStr = Regex.Replace(NewValue, "[\d.\d]", "").Replace("in", """")

                NewValue = NumberInStr.Replace(" ", "") & AlphaInStr.Replace(" ", "")
            Else
                'in
                NumberInStr = Regex.Replace(NewValue, "[^\d.\d]", "")
                AlphaInStr = Regex.Replace(NewValue, "[\d.\d]", "")
                NewValue = NumberInStr.Replace(" ", "") & " " & AlphaInStr.Replace(" ", "")
            End If
        Else
            '如果本来就是字母开头的就算了

        End If
        '已经在调用时，判断过 old new值不等
        Try
            objPipe.Attributes("NominalDiameter").Value = NewValue
            objPipe.Commit()
            If objPipe.Attributes("NominalDiameter").Value = NewValue Then
                Writelog("仪表 [" & TagName & "] " & ", 分支管通径, 由 [" & OldSize & "] 更新为 [" & NewValue & "]", "反向数据流")
            Else
                Writelog("仪表 [" & TagName & "] " & ", 分支管通径, 未能由 [" & OldSize & "] 更新为 [" & NewValue & "]", "反向数据流")
            End If
        Catch
            Writelog("仪表 [" & TagName & "] " & ", 分支管通径, 未能由 [" & OldSize & "] 更新为 [" & NewValue & "]", "反向数据流")

        End Try

        objPipe = Nothing
    End Sub


    Public Function GetSPFPropValue(ByRef obj As IObject, InterfaceName_PropertyName As String, split As String) As String
        Dim sInterface As String : Dim SPROP As String
        Dim pos As Integer = InStr(InterfaceName_PropertyName, split)
        If pos = 0 Then
            '格式不是接口+属性
            Return "Error"
        Else
            sInterface = Mid(InterfaceName_PropertyName, 1, pos - 1)
            SPROP = Mid(InterfaceName_PropertyName, pos + 1)
            If Not IsNothing(obj.Interfaces(sInterface).Properties(SPROP)) Then

                Return obj.Interfaces(sInterface).Properties(SPROP).DisplayValue


            End If
            Return "Null"
        End If


    End Function

End Module
